package com.tianze.library.base;

import android.view.View;

import com.tianze.library.R;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public abstract class BaseClassicRefreshRecyclerFragment extends BaseRefreshFragment implements RecyclerBaseAdapter.OnItemClickListener, RecyclerBaseAdapter.OnItemLongClickListener {

    protected PtrClassicFrameLayout frame;

    @Override
    protected int getLayoutId() {
        return R.layout.tiza_fragment_recycler_classic_refresh;
    }

    @Override
    protected void beforeInitView() {
        frame = ButterKnife.findById(rootView, R.id.ptrFrameLayout);

        frame.setPtrHandler(
                new PtrHandler() {
                    @Override
                    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                    }

                    @Override
                    public void onRefreshBegin(final PtrFrameLayout frame) {
                        refresh();
                    }
                }
        );

        frame.setLastUpdateTimeRelateObject(this);

        frame.postDelayed(
                new Runnable() { //进入自动延迟刷新
                    @Override
                    public void run() {
                        frame.autoRefresh(true);
                    }
                }, 300
        );
    }

    @Override
    protected void onRequestFinish() {
        super.onRequestFinish();
        frame.refreshComplete();
    }

    //TODO 加入了自动刷新,所以这里返回false
    protected boolean requestDataIfViewCreated() {
        return false;
    }
}
